var utils = require('./exports/utils')
var constant = require('./exports/constant')
var point_map = require('./exports/point_map')
var ad_util = require('./exports/ad_util')
var video = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("video:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页视频"])
      );
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("video:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    console.log("video start");
    this.init();
  },
  init() {
    console.log("video init");
    this.execute();
  },
  execute() {
    var duration = constant.video_times;
    var swipe_interval = 500 + random(10, 20);
    while (duration > 0) {
      swipe(
        device.width / 2,
        (device.height / 10) * 9,
        device.width / 2,
        (device.height / 10) * 1,
        swipe_interval
      );
      var interval = 3 + random(1, 3);
      sleep(interval * 1000);

      var flag = random(1, 5);
      console.log(flag, duration);
      if (flag == 1) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log(flag, "找到【相关搜索】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 935, 696, 1062])
          // );
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log(flag, "找到【下一条】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else {
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1005, 696, 1132])
          );
        }
        sleep(2 * 1000);
      }
      if (flag == 3) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log(flag, "找到【相关搜索】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log(flag, "找到【下一条】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else {
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1259, 696, 1386])
          );
        }
        sleep(2 * 1000);
      }

      duration = duration - 1;
    }
    console.log("video execute");
    this.after();
  },
  after() {
    console.log("video after");
    this.next();
  },
  next() {
    console.log("video next");
  },
};

module.exports = video